
package com.productmodule.servlet;
import com.productmodule.bean.TbProductPrice;
import com.productmodule.dao.TbProductPriceDAO;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbProductPriceServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productId=Integer.parseInt(request.getParameter("productId"));
        int productUnitId=Integer.parseInt(request.getParameter("productUnitId"));
        int interfaceId=Integer.parseInt(request.getParameter("interfaceId"));
        String productPirce=request.getParameter("productPirce");
        int creditRatingId=Integer.parseInt(request.getParameter("creditRatingId"));
            if(new TbProductPriceDAO().add(new TbProductPrice(companyId,productId,productUnitId,interfaceId,productPirce,creditRatingId,userId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
        goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_ProductPrice", "CompanyProductMiddleId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int companyProductMiddleId=Integer.parseInt(request.getParameter("companyProductMiddleId"));
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productId=Integer.parseInt(request.getParameter("productId"));
        int productUnitId=Integer.parseInt(request.getParameter("productUnitId"));
        int interfaceId=Integer.parseInt(request.getParameter("interfaceId"));
        String productPirce=request.getParameter("productPirce");
        int creditRatingId=Integer.parseInt(request.getParameter("creditRatingId"));
            if(new TbProductPriceDAO().edit(new TbProductPrice(companyProductMiddleId,companyId,productId,productUnitId,interfaceId,productPirce,creditRatingId,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
           goList(request, response);
        }
        if ("list".equals(method))  goList(request, response);
        if ("listById".equals(method)) {
             request.getSession(true).setAttribute("tbProductPrice",new TbProductPriceDAO().getTbProductPriceByCompanyProductMiddleId(Integer.parseInt(request.getParameter("companyProductMiddleId"))));
            response.sendRedirect("ProductModule/TbProductPriceOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession(true).removeAttribute("tbProductPrice");
            response.sendRedirect("ProductModule/TbProductPriceOperate.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbProductPriceList",new TbProductPriceDAO().getTbProductPriceAll());
        response.sendRedirect("ProductModule/TbProductPriceList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
